package org.example.paymentservice.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Configuration
@RequiredArgsConstructor
public class PaymentConfig {
    private final PaymentProperties paymentProperties;

    @Bean
    public WXPay wxPay() throws Exception {
        WXPayConfig config = new WXPayConfig() {
            @Override
            public String getAppID() {
                return paymentProperties.getWechatPay().getAppId();
            }

            @Override
            public String getMchID() {
                return paymentProperties.getWechatPay().getMchId();
            }

            @Override
            public String getKey() {
                return paymentProperties.getWechatPay().getApiKey();
            }

            @Override
            public InputStream getCertStream() {
                // 如果需要证书，这里需要实现证书加载
                return new ByteArrayInputStream(new byte[0]);
            }
        };
        return new WXPay(config);
    }

    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
            "https://openapi.alipay.com/gateway.do",
            paymentProperties.getAliPay().getAppId(),
            paymentProperties.getAliPay().getPrivateKey(),
            "json",
            "UTF-8",
            paymentProperties.getAliPay().getPublicKey(),
            "RSA2"
        );
    }
}